package org.dromara.xhlj.crud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.service.xhlj.XhljSignTranslationService;
    import org.dromara.common.mybatis.core.page.TableDataInfo;
    import org.dromara.common.mybatis.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.xhlj.domain.bo.XhljSignBo;
import org.dromara.xhlj.domain.vo.XhljSignVo;
import org.dromara.xhlj.domain.XhljSign;
import org.dromara.xhlj.mapper.XhljSignMapper;
import org.dromara.xhlj.crud.service.IXhljSignCrudService;
import org.dromara.xhlj.domain.query.XhljSignQuery;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.HashMap;
/**
 * 大会签到Service业务层处理
 *
 * @author yf
 * @date 2024-07-28
 */
@RequiredArgsConstructor
@Service
public class XhljSignCrudServiceImpl implements IXhljSignCrudService, XhljSignTranslationService {

    private final XhljSignMapper baseMapper;

    /**
     * 查询大会签到
     *
     * @param signId 主键
     * @return 大会签到
     */
    @Override
    public XhljSignVo queryById(Integer signId){
        return baseMapper.selectVoById(signId);
    }


    /**
     * 分页查询大会签到列表
     *
     * @param query        查询条件
     * @param pageQuery 分页参数
     * @return 大会签到分页列表
     */
     @Override
     public TableDataInfo<XhljSignVo> queryPageList(XhljSignQuery query, PageQuery pageQuery) {
        LambdaQueryWrapper<XhljSign> lqw = buildQueryWrapper(query);
        Page<XhljSignVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
     }


    /**
     * 查询符合条件的大会签到列表
     *
     * @param query 查询条件
     * @return 大会签到列表
     */
    @Override
    public List<XhljSignVo> queryList(XhljSignQuery query) {
        LambdaQueryWrapper<XhljSign> lqw = buildQueryWrapper(query);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XhljSign> buildQueryWrapper(XhljSignQuery query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<XhljSign> lqw = Wrappers.lambdaQuery();
                    lqw.eq(query.getSignId() != null, XhljSign::getSignId, query.getSignId());
                    lqw.eq(query.getConferenceInfoId() != null, XhljSign::getConferenceInfoId, query.getConferenceInfoId());
                    lqw.eq(query.getCreateBy() != null, XhljSign::getCreateBy, query.getCreateBy());

                    lqw.like(StringUtils.isNotBlank(query.getSignContent()), XhljSign::getSignContent, query.getSignContent());
        return lqw;
    }



    /**
     * 新增大会签到
     *
     * @param bo 大会签到
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(XhljSignBo bo) {
        XhljSign add = MapstructUtils.convert(bo, XhljSign.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSignId(add.getSignId());
        }
        return flag;
    }

    /**
     * 修改大会签到
     *
     * @param bo 大会签到
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(XhljSignBo bo) {
        XhljSign update = MapstructUtils.convert(bo, XhljSign.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(XhljSign entity){
        //SAIL 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除大会签到信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //SAIL 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 翻译 大会签到 List
     *
     * @param id 外键
     * @return xhljSignVoList
     */
    @Override
    @Cacheable(cacheNames = XhljSign.TRANS_ID_TO_VO_LIST, key = "#id")
    public Object selectVoListById(Integer id,String fieldName){
         Map<String, Object> query = new HashMap<>();
         query.put(fieldName, id);
         return baseMapper.selectVoByMap(query);
    }

    /**
     * 翻译 大会签到
     *
     * @param id 主键
     * @return xhljSign
     */
    @Override
    @Cacheable(cacheNames = XhljSign.TRANS_ID_TO_VO, key = "#id")
    public Object selectVoById(Integer id){
        return baseMapper.selectVoById(id);
    }
}
